home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17233 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  32 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c++,comp.lang.c,comp.os.ms-windows.programmer.misc
  4. Subject: Re: fastest code
  5. Date: 14 Apr 1996 10:10:13 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4krbhlINNbk4@keats.ugrad.cs.ubc.ca>
  8. References: <316112A2.7D37@public.sta.net.cn> <4kjpn0INN817@keats.ugrad.cs.ubc.ca> <829352535snz@genesis.demon.co.uk> <4kof6e$te@news1.mnsinc.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4kof6e$te@news1.mnsinc.com>,
  12. Szu-Wen Huang <huang@mnsinc.com> wrote:
  13.  
  14.  >One would almost always expect addition to be faster than multiplication.
  15.  >An optimizing compiler might be able to realize they are actually the
  16.  >same, and perhaps even decide to implement both as bitwise shifts.  In
  17.  >any case, I wouldn't expect the former to be slower than the latter.  (It
  18.  >certainly *might* be, but chances are slim).
  19.  
  20. I _would_ expect the latter to be faster. In fact when I first encountered the
  21. code, I thought to myself, ``why isn't this written as 2*i''?
  22.  
  23. Why I would expect it to have a beter chance at being faster is that
  24. multiplication by a constant is a common subject of optimization. Adding a
  25. quantity to itself is not a common idiom, hence it's not an important
  26. optimization to implement. The gain by doing strength reduction from a
  27. mulitiplication to an addition is far more important than any gain in speed you
  28. could squeeze out of optimizing simple additions.
  29.  
  30. If I were writing a compiler, I wouldn't bother ``optimizing'' this case at
  31. all: If the programmer thinks he knows better, let him have his addition!
  32.